<?php

class Subscribers extends CActiveRecord
{
	/**
	 * The followings are the available columns in table 'Subscribers':
	 * @var integer $id
	 * @var string $name
	 * @var string $email
	 * @var string $affiliate
	 * @var integer $mlId
	 */

	/**
	 * Returns the static model of the specified AR class.
	 * @return CActiveRecord the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'Subscribers';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		return array(
			array('name, email, affiliate','required'),
			array('name', 'length', 'max'=>128),
			array('name', 'uniqueInGroupI', 'on'=>'insert'), // unique in the group
			array('name', 'uniqueInGroupU', 'on'=>'update'), // unique in the group except myself
			array('email','email'),
			array('email', 'uniqueInGroupI', 'on'=>'insert'), // unique in the group
			array('email', 'uniqueInGroupU', 'on'=>'update'), // unique in the group except myself
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'ml' => array(self::BELONGS_TO, 'MalingLists', 'mlId'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'Id',
			'name' => '氏名',
			'email' => 'Email',
			'affiliate' => '所属',
			'mlId' => 'メーリングリスト',
			'create_time' => '登録日時',
			'update_time' => '更新日時',
		);
	}

        public function uniqueInGroupI($item) {
		// Yii::trace(get_class($this).'.uniqueInGroupI()');
		$criteria=new CDbCriteria;
		$criteria->condition = "`mlId` = '{$_GET['mlId']}'";
		$criteria->condition .= " AND `{$item}` = '{$this->{$item}}'";
		$userCount=$this->count($criteria->condition);
		if ($userCount) {
			$this->addError($item, ucfirst($item).' "'.$this->{$item}.'" has already been taken.');
		}
        }

        public function uniqueInGroupU($item) {
		// Yii::trace(get_class($this).'.uniqueInGroupU()');
		$criteria=new CDbCriteria;
		$criteria->condition = "`mlId` = '{$_GET['mlId']}'";
		$criteria->condition .= " AND `{$item}` = '{$this->{$item}}'";
		$criteria->condition .= " AND `id` <> '{$_GET['id']}'";
		$userCount=$this->count($criteria->condition);
		if ($userCount) {
			$this->addError($item, ucfirst($item).' "'.$this->{$item}.'" has already been taken.');
		}
        }

        /**
         * @return array behaviors
         */
        public function behaviors()
        {
		return array(
			'CTimestampBehavior' => array(
				'class' => 'zii.behaviors.CTimestampBehavior',
			),
		);
        }

	public function scopes($p)
	{
		Yii::trace(get_class($this).'.scopes()'.$p);
		return array(
			     'ofml'=>array(
					 'condition'=>'mlId=',
					 ),
			     );
	}
}